#!/usr/bin/python
# -*- coding: iso-8859-1 -*-

#-----------------------------------------
# BusyManager.py is a part of mahjong-2d project.
# Name:         BusyManager.py
# Author:       Matthieu Morillon
# Copyright:    (C) 2013 Matthieu Morillon, Antoine Morillon
# Licence:      GNU General Public Licence version 3
# Website:      
# Email:        
#-----------------------------------------

import os

class BusyManager:

    def __init__(self, widget):
        self.toplevel = widget.winfo_toplevel()
        self.widgets = {}

    def busy(self, widget=None):

        # attach busy cursor to toplevel, plus all windows
        # that define their own cursor.
        print "busy"
        print widget
        
        custom = "@"+os.getcwd()+"/Cursor_high2.cur" # "@Cursor_av_offset.cur"
        

        if widget is None:
            w = self.toplevel # myself
            print "None"
        else:
            w = widget

        if not self.widgets.has_key(str(w)):
            try:
                # attach cursor to this widget
                print "try"
                cursor = w.cget("cursor")
                if cursor != custom:
                    print "Pas le bon curseur [%s]VS[%s]"%(cursor,custom)
                    self.widgets[str(w)] = (w, cursor)
                    w.config(cursor=custom)
            except TclError:
                print "except"
                pass

        for w in w.children.values():
            self.busy(w)

    def notbusy(self):
        # restore cursors
        for w, cursor in self.widgets.values():
            try:
                w.config(cursor=cursor)
            except TclError:
                pass
        self.widgets = {}
